package com.healthai.controller;

import com.healthai.dto.request.DietRecordRequest;
import com.healthai.dto.response.ApiResponse;
import com.healthai.entity.DietRecord;
import com.healthai.entity.User;
import com.healthai.service.DietRecordService;
import com.healthai.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/api/diet")
public class DietRecordController {
    
    @Autowired
    private DietRecordService dietRecordService;
    @Autowired
    private UserService userService;

    /**
     * 获取当前用户ID
     * @return 用户ID
     */
    private Integer getCurrentUserId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        // 这里需要从用户名获取用户ID，可以通过UserService实现
        User user = userService.getUserByUsername(username);
        return user.getUserId();
    }

    /**
     * 创建饮食记录
     *
     * @param request 饮食记录请求
     * @return 记录ID
     */
    @PostMapping("/record")
    public ApiResponse<Integer> createDietRecord(@Validated @RequestBody DietRecordRequest request) {
        request.setDietDate(LocalDate.now());
        Integer userId = getCurrentUserId();
        Integer recordId = dietRecordService.createDietRecord(userId, request);
        return ApiResponse.success("创建成功", recordId);
    }
    
    /**
     * 更新饮食记录
     * @param recordId 记录ID
     * @param request 饮食记录请求
     * @return 是否成功
     */
    @PutMapping("/record/{recordId}")
    public ApiResponse<Boolean> updateDietRecord(@PathVariable Integer recordId, 
                                               @Validated @RequestBody DietRecordRequest request) {
        Integer userId = getCurrentUserId();
        boolean success = dietRecordService.updateDietRecord(userId, recordId, request);
        return ApiResponse.success("更新成功", success);
    }
    
    /**
     * 获取某日期的饮食记录
     * @param date 日期
     * @return 饮食记录列表
     */
    @GetMapping("/records")
    public ApiResponse<List<DietRecord>> getDietRecordsByDate(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
        Integer userId = getCurrentUserId();
        List<DietRecord> records = dietRecordService.getDietRecordsByDate(userId, date);
        return ApiResponse.success(records);
    }
    
    /**
     * 获取日期范围内的饮食记录
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 饮食记录列表
     */
    @GetMapping("/records/range")
    public ApiResponse<List<DietRecord>> getDietRecordsByDateRange(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        Integer userId = getCurrentUserId();
        List<DietRecord> records = dietRecordService.getDietRecordsByDateRange(userId, startDate, endDate);
        return ApiResponse.success(records);
    }
    
    /**
     * 获取最近的饮食记录
     * @param limit 限制数量
     * @return 饮食记录列表
     */
    @GetMapping("/records/recent")
    public ApiResponse<List<DietRecord>> getRecentDietRecords(
            @RequestParam(defaultValue = "10") Integer limit) {
        Integer userId = getCurrentUserId();
        List<DietRecord> records = dietRecordService.getRecentDietRecords(userId, limit);
        return ApiResponse.success(records);
    }
    
    /**
     * 删除饮食记录
     * @param recordId 记录ID
     * @return 是否成功
     */
    @DeleteMapping("/record/{recordId}")
    public ApiResponse<Boolean> deleteDietRecord(@PathVariable Integer recordId) {
        Integer userId = getCurrentUserId();
        boolean success = dietRecordService.deleteDietRecord(userId, recordId);
        return ApiResponse.success("删除成功", success);
    }
}